<?php


namespace app\model\hotel;

use app\model\BaseModel;

class HotelApply extends BaseModel
{
    //入驻
    public function apply($data,$member_id)
    {
        $data[ 'create_time' ] = time();

        //$hotel_id = model('hotel')->add($data);
        //获取商家申请信息
        $apply_info = model('hotel_apply')->getInfo([ [ 'member_id', '=', $member_id ] ]);

        if ($apply_info) {
            //判断认证信息是否存在
            $res = model('hotel_apply')->update($data, [ [ 'member_id', '=', $member_id ] ]);

        } else {

            $res = model('hotel_apply')->add($data);
        }

        return $this->success($res);
    }

    /**
     * 获取申请信息(不包含认证信息)
     * @param $condition
     * @param string $field
     * @return array
     */
    public function getApplyInfo($condition, $field = '*')
    {
        $info = model('hotel_apply')->getInfo($condition, $field);
        return $this->success($info);
    }


    /**
     * 获取店铺申请列表
     * @param array $condition
     * @param string $field
     * @param string $order
     * @param string $limit
     */
    public function getApplyList($condition = [], $field = '*', $order = '', $limit = null)
    {

        $list = model('hotel_apply')->getList($condition, $field, $order, '', '', '', $limit);
        return $this->success($list);
    }

    /**
     * 获取店铺申请分页列表
     * @param array $condition
     * @param number $page
     * @param string $page_size
     * @param string $order
     * @param string $field
     */
    public function getApplyPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
    {
        $list = model('hotel_apply')->pageList($condition, $field, $order, $page, $page_size);
        return $this->success($list);
    }

    /**
     * 修改店铺申请
     * @param array $data
     */
    public function editApply($data, $condition)
    {
        $res = model('hotel_apply')->update($data, $condition);
        return $this->success($res);
    }

    public function applyPass($apply_id,$data,$user_info,$commission_rate,$direct)
    {
        model('hotel_apply')->startTrans();

        try {

            if ($data['apply_state']==1){
                $hotel_apply_info = model('hotel_apply')->getInfo( [ [ 'apply_id', '=', $apply_id ] ]);

                $hotel_data = [
                    'hotel_type'=>$hotel_apply_info['hotel_type'],
                    'member_id'=>$hotel_apply_info['member_id'],
                    'hotel_name'=>$hotel_apply_info['hotel_name'],
                    'hotel_img'=>$hotel_apply_info['hotel_img'],
                    'hotel_level'=>$hotel_apply_info['hotel_level'],
                    'province_id'=>$hotel_apply_info['province_id'],
                    'city_id'=>$hotel_apply_info['city_id'],
                    'district_id'=>$hotel_apply_info['district_id'],
                    'full_address'=>$hotel_apply_info['full_address'],
                    'address'=>$hotel_apply_info['address'],
                    'longitude'=>$hotel_apply_info['longitude'],
                    'latitude'=>$hotel_apply_info['latitude'],
                    'start_time'=>$hotel_apply_info['start_time'],
                    'end_time'=>$hotel_apply_info['end_time'],
                    'telphone'=>$hotel_apply_info['telphone'],
                    'name'=>$hotel_apply_info['name'],
                    'images'=>$hotel_apply_info['images'],
                    'front_pic_id_card'=>$hotel_apply_info['front_pic_id_card'],
                    'reverse_pic_id_card'=>$hotel_apply_info['reverse_pic_id_card'],
                    'id_card_name'=>$hotel_apply_info['id_card_name'],
                    'id_card_number'=>$hotel_apply_info['id_card_number'],
                    'id_card_type'=>$hotel_apply_info['id_card_type'],
                    'id_card_start_time'=>$hotel_apply_info['id_card_start_time'],
                    'id_card_end_time'=>$hotel_apply_info['id_card_end_time'],
                    'account_holder'=>$hotel_apply_info['account_holder'],
                    'bank_number'=>$hotel_apply_info['bank_number'],
                    'bank_type'=>$hotel_apply_info['bank_type'],
                    'bank_name'=>$hotel_apply_info['bank_name'],
                    'bank_img'=>$hotel_apply_info['bank_img'],
                    'business_license'=>$hotel_apply_info['business_license'],
                    'business_license_number'=>$hotel_apply_info['business_license_number'],
                    'business_license_type'=>$hotel_apply_info['business_license_type'],
                    'business_license_start_time'=>$hotel_apply_info['business_license_start_time'],
                    'business_license_end_time'=>$hotel_apply_info['business_license_end_time'],
                    'commission_rate'=>$commission_rate,
                    'direct'=>$direct,
                    'status'=>1,

                    'contact_email'=>$hotel_apply_info['contact_email'],
                    'legal_addr'=>$hotel_apply_info['legal_addr'],
                    'legal_cert_validity_type'=>$hotel_apply_info['legal_cert_validity_type'],
                    'busi_type'=>$hotel_apply_info['busi_type'],
                    'bank_province'=>$hotel_apply_info['bank_province'],
                    'bank_city'=>$hotel_apply_info['bank_city'],
                    'bank_district'=>$hotel_apply_info['bank_district'],
                    'bank_province_name'=>$hotel_apply_info['bank_province_name'],
                    'bank_city_name'=>$hotel_apply_info['bank_city_name'],
                    'bank_district_name'=>$hotel_apply_info['bank_district_name'],
                ];

                $hotel_model = new Hotel();

                $shop_res = $hotel_model->addHotel($hotel_data, $user_info);


                if ($shop_res[ 'code' ] < 0) {
                    model('hotel_apply')->rollback();
                    return $shop_res;
                }
                $data['hotel_id'] = $shop_res['data'];
            }

            $res = model('hotel_apply')->update($data, [ [ 'apply_id', '=', $apply_id ] ]);

            model("hotel_apply")->commit();
            return $this->success($res);
        } catch (\Exception $e) {
            model("hotel_apply")->rollback();
            return $this->error("", $e->getMessage());
        }

    }
}